<?php

namespace app\payment\admin;

use app\common\builder\ZBuilder;
use app\payment\model\WithdrawalApi as WithdrawalApiModel;
use app\payment\model\PaymentWithdrawalCompany as PaymentWithdrawalCompanyModel;
use app\payment\model\Download as DownloadModel;
use app\payment\model\Mconfig as MconfigModel;
use app\api\model\PaymentWithdrawalApi as PaymentWithdrawalApiModel;
use think\facade\Env;
use think\facade\Request;
use think\Db;

/**
 * 提现接口控制器
 */
class WithdrawalApi extends Index
{

    protected function initialize()
    {
        parent::initialize();
        if ($this->no_auth) {
            $this->error('权限不足！');
        }

        if (in_array(request()->action(), ['add'])) {
            if (!$this->add_safe) {
                $this->error('NO');
            }
        }
        if (in_array(request()->action(), ['edit'])) {
            if (!$this->edit_safe) {
                $this->error('NO');
            }
        }
        if (in_array(request()->action(), ['delete'])) {
            if (!$this->delete_safe) {
                $this->error('NO');
            }
        }
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        // 获取查询条件
        $map = $this->getMap();

        if ($map) {
            foreach ($map as $map_k1 => $map_v1) {

                if ($map_v1[0] == 'cash') {
                    $map[$map_k1][2] = $map[$map_k1][2] * 100;
                }
                if ($map_v1[0] == 'now_cash') {
                    $map[$map_k1][2] = $map[$map_k1][2] * 100;
                }
                if ($map_v1[0] == 'merchant_id') {
                    $search_merchant_id_key = $map_k1;
                }
            }
        }

        $search_merchant_id_key = isset($search_merchant_id_key) ? $search_merchant_id_key : count($map);

        $all_withdrawal_status = [1 => '处理中', 2 => '成功', 3 => '失败', 4 => '关闭',];
        if ($this->no_auth) {
            $map[$search_merchant_id_key] = ['merchant_id', '=', $this->merchant_id];
        }

        $list_rows = input('list_rows', 20) >= 20 ? 20 : input('list_rows');
        // 数据列表
        $data_list = WithdrawalApiModel::where($map)->order('create_time desc')->paginate($list_rows);

        $sum_count = WithdrawalApiModel::where($map)->count();
        $sum_cash = WithdrawalApiModel::where($map)->sum('cash');
        $sum_cash /= 100;

        // 分页数据
        $page = $data_list->render();

        $all_bank_type = ['借记卡' => '借记卡', '信用卡' => '信用卡', '贷记卡' => '贷记卡'];
        $all_account_type = ['私户' => '私户', '公户' => '公户'];

        $date = date('Y-m-d');
        $search = [
            ['daterange:6', 'create_time', '发起时间', '', "$date 00:00:00 - $date 23:59:59", ['max-date' => "$date 23:59:59", 'format' => 'YYYY-MM-DD HH:mm:ss', 'time-picker' => 'true', 'time-picker24-hour' => 'true', 'time-picker-seconds' => 'true']],
            ['select:3', 'status', '状态', '', '', $all_withdrawal_status],
            ['select:3', 'bank_type', '银行卡类型', '', '', $all_bank_type],
            ['select:3', 'account_type', '账户类型', '', '', $all_account_type],
            ['text:3', 'bank_card', '收款银行卡'],
            ['text:3', 'name', '收款人姓名'],
            ['text:3', 'mobile', '收款人手机号'],
            ['text:3', 'cash', '金额'],
            ['text:3', 'now_cash', '到账金额'],
            ['text:3', 'order_id', '平台订单号'],
            ['text:3', 'other_order_id', '第三方订单号'],
            ['text:3', 'merchant_id', '商户号'],
        ];

        $items = [
            ['operation_id', '操作', 'callback', function ($value) {
                return $value . '<br/>' . get_user_nickname($value);
            }, '', 'text-center'],
            ['merchant_id', '商户', 'callback', function ($value) {
                return $value . '<br/>' . get_user_nickname($value);
            }, '', 'text-center'],
            ['order_id', '平台订单号'],
            ['name', '收款人姓名'],
            ['bank_card', '收款银行卡'],
            ['area', '地区', 'callback', function ($value) {
                return $value['bank_name'] . $value['province'] . $value['city'] . $value['account_opening_branch'];
            }, '__data__', 'text-center'],
            ['cash', '提现金额（元）', 'callback', function ($cash) {
                return $cash / 100;
            }],
            ['create_time', '创建时间', 'datetime', '', 'Y-m-d H:i:s'],
            ['status', '状态', 'status', '', $all_withdrawal_status],
            ['right_button', '操作', 'btn']
        ];

        $describe = [
            'title' => "总笔数：{$sum_count} 笔    总金额：{$sum_cash} 元",
            'icon' => 'fa fa-fw fa-jpy',
            'class' => 'btn btn-xs btn-info',
        ];
        $export = [
            'title' => '导出',
            'icon' => 'fa fa-fw fa-arrow-circle-o-down',
            'target' => '_blank',
            'class' => 'btn btn-xs btn-default',
            'href' => url('export', ['map' => json_encode($map)]),
            'data-title' => '真的要导出吗？',
            'data-tips' => '导出可能要点时间,请再次点击导出',
        ];

        $add_withdrawal = <<<EOF
<a title="申请接口提现" style="margin-bottom:6px;" class="btn btn-nm btn-primary" href="add" >
<i class="fa fa-plus-circle"></i>
申请接口提现
</a> 
EOF;

        if ($this->no_auth) {
            $info = MconfigModel::where(['id' => $this->merchant_id, 'status' => 1])->find();
            if (false === strpos($info['trade_type'], 'withdrawal')) {
                $add_withdrawal = '';
            }
        }

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('接口提现列表') // 设置页面标题
            ->setTableName('payment_withdrawal_api') // 设置数据表名
            ->setPrimaryKey('order_id') // 设置主键名为id
            ->setSearchArea($search) // 设置搜索参数
            ->setPageTips("")
            ->hideCheckbox()
            ->addColumns($items)
            ->addTopButton('describe', $describe) // 添加按钮
            ->addTopButton('export', $export) // 添加按钮
            ->setExtraHtml($add_withdrawal, 'toolbar_top')
            ->addRightButton('see', ['title' => '查看', 'icon' => 'fa fa-fw fa-eye', 'href' => url('see', ['id' => '__id__'])], true)
            ->addRightButton('verify', ['title' => '验证订单', 'class' => 'btn btn-xs btn-default ajax-get', 'icon' => 'fa fa-fw fa-leaf', 'href' => url('verify', ['id' => '__id__'])])
            ->addRightButtons('edit') // 批量添加右侧按钮
            ->setRowList($data_list) // 设置表格数据
            ->setPages($page) // 设置分页数据
            ->replaceRightButton(['status' => ['not in', '1']], '', 'edit') // 替换右侧按钮
            ->fetch(); // 渲染页面
    }

    /**
     * 导出数据
     * @return mixed
     */
    public function export($map = '')
    {
        ini_set('memory_limit', '1024M');
        ob_end_clean();

        if (!is_dir($this->file_path)) {
            $this->error('数据导出目录不存在！', url('index'));
        }
        set_time_limit(3600);

        if (!empty($map)) {
            $map = htmlspecialchars_decode($map);
            $map = urldecode($map);
            $map = str_replace("+", " ", $map);
            // 获取查询条件
            $map = json_decode($map, true);
        }

        $all_withdrawal_status = [1 => '处理中', 2 => '成功', 3 => '失败', 4 => '关闭',];

        if ($map) {
            foreach ($map as $map_k1 => $map_v1) {

                if ($map_v1[0] == 'merchant_id') {
                    $search_merchant_id_key = $map_k1;
                }
            }
        }

        $search_merchant_id_key = isset($search_merchant_id_key) ? $search_merchant_id_key : count($map);

        if ($this->no_auth) {
            $map[$search_merchant_id_key] = ['merchant_id', '=', $this->merchant_id];
        }

        $count = WithdrawalApiModel::where($map)->count();
        if (empty($count)) {
            $this->error('数据导出为空！', url('index'));
        }

        // 最多导出数量
        $export_order_max_count = pay_config('export_order_max_count');
        if ($count > $export_order_max_count) {
            $this->error("系统设置一次最多导出{$export_order_max_count}条数据！");
        }

        $date = date('Y-m-d');
        // 商户每天最多导出订单次数
        $order_export_day_count = pay_config('order_export_day_count');
        $order_map[] = ['merchant_id', '=', $this->merchant_id];
        $order_map[] = ['create_time', 'between time', ["$date 00:00:00", "$date 23:59:59"]];
        $download_count = DownloadModel::where($order_map)->count();
        if ($download_count >= $order_export_day_count) {
            $this->error("系统设置商户每天最多导出订单次数是{$order_export_day_count}次！");
        }

        $download['id'] = date('YmdHis') . rand(100000, 999999);
        $download['merchant_id'] = $this->merchant_id;
        $download['title'] = "接口提现订单导出条数（{$count}）";
        $download['file_name'] = $download['id'] . '.csv';
        $download['url'] = Request::module() . '/' . Request::controller() . '/' . Request::action();
        $download['status'] = 2;
        $download['programmer_remark'] = json_encode($map);
        $download_insert = DownloadModel::create($download);
        if (!$download_insert) {
            $this->error("导出失败！");
        }

        $field = [
            'operation_id',
            'merchant_id',
            'bank_type',
            'bank_name',
            'name',
            'id_card',
            'mobile',
            'bank_card',
            'account_type',
            'account_opening_branch',
            'couplet_number',
            'province',
            'city',
            'cash',
            'now_cash',
            'status',
            'order_id',
            'other_order_id',
            'create_time',
            'update_time',
        ];
        if ($this->no_auth) {
            unset($field[0]);
        }

        $cellName = [
            '操作号',
            '商户号',
            '银行卡类型',
            '收款银行名称',
            '收款人姓名',
            '收款人身份证',
            '收款人手机号',
            '收款银行卡',
            '账户类型',
            '开户支行',
            '联行号',
            '省份名称',
            '城市名称',
            '提现金额',
            '到账金额',
            '状态',
            '本地订单号',
            '第三方订单号',
            '创建时间',
            '更新时间',
        ];
        if ($this->no_auth) {
            unset($cellName[0]);
        }
        $fp = fopen($download['file_name'], 'w');

        foreach ($cellName as $key => $value) {
            $cellName[$key] = mb_convert_encoding($cellName[$key], 'gb2312', 'utf-8');
        }

        fputcsv($fp, $cellName);

        Db::name('payment_withdrawal_api')
            ->where($map)
            ->field($field)
            ->chunk(200, function ($all) use ($fp, $all_withdrawal_status) {
                foreach ($all as $single) {

                    $single['cash'] /= 100;
                    $single['now_cash'] /= 100;
                    $single['status'] = $all_withdrawal_status[$single['status']];
                    $single['create_time'] = date('Y-m-d H:i:s', $single['create_time']);
                    $single['update_time'] = date('Y-m-d H:i:s', $single['update_time']);

                    foreach ($single as $key => $value) {
                        if (!in_array($key, ['cash', 'merchant_id'])) {
                            $single[$key] = "\t" . mb_convert_encoding($value, 'gb2312', 'utf-8');
                        }
                    }

                    fputcsv($fp, $single);
                    unset($single);
                }
            }, 'order_id', 'desc');
        fclose($fp);

        DownloadModel::update(['id' => $download['id'], 'status' => 1, 'update_time' => time()]);
        rename(Env::get('root_path') . 'public' . DIRECTORY_SEPARATOR . $download['file_name'], $this->file_path . $download['file_name']); // 移动到新目录

        $this->success('导出成功', url('download/index'));
    }

    /**
     * 新增
     * @return mixed
     */
    public function add()
    {
        // 是否开启手动提现
        if ($this->merchant_info['manual_withdrawal'] == 0) {
            $this->error("无提现权限！", url('index'));
        }

        $mconfig = MconfigModel::where(['id' => $this->merchant_id, 'status' => 1])->find();
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($this->merchant_info['trade_pwd'] != md5(md5(md5($data['trade_pwd'])))) {
                $this->error('交易密码不正确！');
            }

            unset($data['trade_pwd']);

            // 验证
            $result = $this->validate($data, 'WithdrawalApi');
            // 验证失败 输出错误信息
            if (true !== $result) {
                $this->error($result);
            }

            if (false === strpos($mconfig['trade_type'], 'withdrawal')) {
                $this->error('您没有开通提现功能！');
            }

            $data['cash'] *= 100;                                        // 金额
            $data['status'] = 1;                                         // 申请状态
            $data['create_time'] = time();                               // 创建时间
            $data['update_time'] = time();                               // 更新时间
            $data['operation_id'] = $this->merchant_id;                  // 操作号
            $data['company'] = strstr($data['child_company'], 'Withdrawal', true);

            $info = WithdrawalApiModel::where('order_id', $data['order_id'])->find();
            if ($info) {
                $this->error("请刷新，数据已经存在！");
            }

            if ($temp = WithdrawalApiModel::create($data)) {

                $company = strstr($data['child_company'], '_', true);
                $data['cash'] /= 100;
                $children_company_config = PaymentWithdrawalCompanyModel::where(['name' => $data['child_company']])->find()->toArray();
                $payment_withdrawal_obj = new PaymentWithdrawalApiModel();
                $resarray = $payment_withdrawal_obj->$company($data, $children_company_config);
                // 状态：1:处理中 2:成功 3:失败 4:关闭 
                $update['status'] = $resarray['code'] == 200 ? 1 : 3;
                $update['other_order_id'] = isset($resarray['other_order_id']) ? $resarray['other_order_id'] : '';
                $update['return_param'] = isset($resarray['return_param']) ? substr($resarray['return_param'], 0, 60000) : '';
                $update['update_time'] = time();

                $msg = isset($resarray['msg']) ? $resarray['msg'] : '未知错误';
                $msg = '上游提示：' . $msg;
                // 捕获异常,订单表数据改为失败
                try {
                    WithdrawalApiModel::where(['order_id' => $data['order_id']])->update($update);
                } catch (\Exception $e) {
                    payment_error_log(860000, $data, $e->getMessage());
                }

                // 记录行为
                action_log('payment_withdrawal_api_add', 'withdrawal', $temp['order_id'], UID, json_encode($temp));
                $this->success($msg, url('index'));
            } else {
                $this->error('新增失败！');
            }
        }

        $all_withdrawal_company = PaymentWithdrawalCompanyModel::where(['status' => 1])->column('name,title');

        $order_id = date('ymdHis') . random(8);
        $items = [
            ['select', 'merchant_id', '收款商户号', '', all_merchant()],
            ['select', 'bank_type', '银行卡类型', '', ['借记卡' => '借记卡', '信用卡' => '信用卡', '贷记卡' => '贷记卡']],
            ['text', 'bank_name', '收款银行名称', '必填：比如:<code>中国工商银行</code>'],
            ['text', 'name', '收款人姓名', '必填：比如:<code>我是谁</code>'],
            ['text', 'id_card', '收款人身份证', '必填：不清楚填0<code>身份证号码</code>'],
            ['text', 'mobile', '收款人手机号', '可填：比如:<code>13888888888</code>说明：如果填写，提现成功会给此手机号发送短信。'],
            ['text', 'bank_card', '收款银行卡', '必填：比如:<code>6217002870051515818</code>'],
            ['text', 'account_opening_branch', '开户支行', '必填：比如:<code>工商银行广州高新支行</code>'],
            ['radio', 'account_type', '账户类型', '', ['私户' => '私户', '公户' => '公户'], '私户'],
            ['text', 'couplet_number', '联行号', '<code>公户</code>账户必须填写：比如:<code>103521003839</code>：<a target="_blank" href="https://www.hebbank.com/corporbank/otherBankQueryWeb.do">查看地址</a>'],
            ['text', 'province', '省份名称', '必填：比如:<code>广东省</code>'],
            ['text', 'city', '城市名称', '必填：比如:<code>广州市</code>'],
            ['text', 'cash', '提现金额', '必填（整数）：比如:<code>10000</code>'],
            ['textarea', 'merchant_remark', '商户备注'],
            ['text', 'extra', '其他参数'],
            ['select', 'child_company', '选择提现公司', '', $all_withdrawal_company],
            ['static', 'order_id', '订单号', '提现订单号', $order_id, $order_id],
            ['password', 'trade_pwd', '交易密码', '6-16位'],
        ];

        $tips = "请勿填错！";

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('申请接口提现') // 设置页面标题
            ->addFormItems($items)
            ->setPageTips($tips)
            ->fetch();
    }

    /**
     * 验证订单
     * @param  null      $id    平台订单号
     * @param  boolean   $rule  返回类型
     * @return mixed
     */
    public function verify($id = null, $rule = false)
    {
        if ($id === null) {
            $this->error('缺少参数！');
        }

        $info = WithdrawalApiModel::where(['order_id' => $id])->find();
        if (!$info) {
            $this->error('平台订单号不存在！');
        }

        $child_config = PaymentWithdrawalCompanyModel::where(['name' => $info['child_company']])->column('*', 'name');
        $company_merge_values = $child_config[$info['child_company']];

        $order_param = $info['order_id'];
        $company_parent_name = $info['company'];
        if (in_array($company_parent_name, ['abcdefg'])) {
            $order_param = $info['other_order_id'] ?: $info['order_id'];
        }

        $companys = $company_parent_name . 'WithdrawalQuery';
        // 实例化第三方订单模型
        $other_order_obj = new PaymentWithdrawalApiModel();
        // 发送给第三方支付公司订单号    
        $resarray = $other_order_obj->$companys($order_param, $company_merge_values, false, $info->toArray());
        $return_param = format_nice($resarray['return_param']);
        $res_arr = json_decode($return_param, true);
        $msg = '未知错误';
        $find = 0;

        if (isset($res_arr['status']) && $company_parent_name == 'fuyitong') {
            $find = 1;
            switch ($res_arr['status']) {
                case '1':
                    $msg = '成功';
                    break;
                case '2':
                    $msg = '失败';
                    break;
                case '0':
                    $msg = '银行处理中';
                    break;
                case '202':
                    $find = 0;
                    break;
            }
        }

        if (isset($res_arr['trade_state']) && $company_parent_name == 'youjuhe') {
            $find = 1;
            switch ($res_arr['trade_state']) {
                case 'SUCCESS':
                    $msg = '成功';
                    break;
                case 'PAYERROR':
                    $msg = '失败';
                    break;
                case 'REVOKED':
                    $msg = '失败';
                    break;
                case 'NOTPAY':
                    $msg = '银行处理中';
                    break;
                case 'PROCESSING':
                    $msg = '银行处理中';
                    break;
            }
        }


        if ($rule) {
            return $find;
        }


        $resdata = <<<EOF
<div class='col-xs-12'>
<textarea class='form-control' rows='20' style='margin: 0px; height: 500px; width: 1000px;'>
$return_param
</textarea>
</div>                    
EOF;

        $this->error("提示：{$msg}！第三方返回结果为：{$resdata}");
    }

    /**
     * 查看
     * @param null $id 
     * @return mixed
     */
    public function see($id = null)
    {
        if ($id === null) {
            $this->error('缺少参数');
        }
        $all_withdrawal_status = [1 => '处理中', 2 => '成功', 3 => '失败', 4 => '关闭',];
        // 获取数据
        $info = WithdrawalApiModel::where('order_id', $id)->field(true)->find();
        if (!$info) {
            $this->error('不存在！');
        }

        if ($this->no_auth) {
            if ($this->merchant_id != $info['merchant_id']) {
                $this->error('权限不足！');
            }
        }

        // 所有的提现公司
        $all_config_group = pay_config('withdrawal_company');
        // 所有的提现公司子商户号
        $all_child_company = PaymentWithdrawalCompanyModel::order(['name' => 'asc'])->column('title', 'name');

        $info['cash'] /= 100;
        $info['now_cash'] /= 100;
        $info['company'] = isset($all_config_group[$info['company']]) ? $all_config_group[$info['company']] : '';
        $info['child_company'] = isset($all_child_company[$info['child_company']]) ? $all_child_company[$info['child_company']] : '';
        $info['return_param'] = format_nice($info['return_param']);
        $info['notify_param'] = format_nice($info['notify_param']);
        $items = [
            ['static', 'merchant_id', '商户号', '商户名称：' . get_user_nickname($info['merchant_id'])],
            ['static', 'bank_type', '银行卡类型', '', ['借记卡' => '借记卡', '信用卡' => '信用卡', '贷记卡' => '贷记卡']],
            ['static', 'bank_name', '收款银行名称'],
            ['static', 'name', '收款人姓名'],
            ['static', 'id_card', '收款人身份证'],
            ['static', 'mobile', '收款人手机号'],
            ['static', 'bank_card', '收款银行卡'],
            ['static', 'account_type', '账户类型', '', ['私户' => '私户', '公户' => '公户'], '私户'],
            ['static', 'account_opening_branch', '开户支行'],
            ['static', 'couplet_number', '联行号'],
            ['static', 'province', '省份名称'],
            ['static', 'city', '城市名称'],
            ['textarea', 'merchant_remark', '商户备注[:无]', '', '', 'disabled', ''],
            ['radio', 'status', '状态', '', $all_withdrawal_status, '', '', 'disabled'],
            ['static', 'cash', '提现金额（元）'],
            ['static', 'now_cash', '到账金额（元）'],
            ['static', 'order_id', '平台订单号'],
            ['textarea', 'remark', '本地备注[:无]', '', '', 'disabled', ''],
            ['static', 'other_order_id', '第三方订单号'],
            ['textarea', 'return_param', '返回数据[:无]', '', '', 'disabled', ''],
            ['textarea', 'notify_param', '通知数据[:无]', '', '', 'disabled', ''],
            ['static', 'company', '提现公司', '', '', '', ''],
            ['static', 'child_company', '提现公司子商户号'],
            ['datetime', 'create_time', '创建时间', '', '', 'YYYY-MM-DD HH:mm:ss', 'disabled'],
            ['datetime', 'update_time', '更新时间', '', '', 'YYYY-MM-DD HH:mm:ss', 'disabled'],
            ['static', 'operation_id', '操作号', '商户名称：' . get_user_nickname($info['operation_id'])],
        ];

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('查看') // 设置页面标题
            ->addFormItems($items)
            ->hideBtn('submit')
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }

    /**
     * 编辑
     * @param null $id  id
     * @return mixed
     */
    public function edit($id = null)
    {
        if ($this->no_auth) {
            $this->error('权限不足！');
        }

        if ($id === null) {
            $this->error('缺少参数！');
        }

        // 获取数据
        $info = WithdrawalApiModel::where('order_id', $id)->field(true)->find();
        if (!$info) {
            $this->error('不存在！');
        }

        if (!in_array($info['status'], [1])) {
            $this->error('提现状态不是（处理中）状态！');
        }

        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($this->merchant_info['trade_pwd'] != md5(md5(md5($data['trade_pwd'])))) {
                $this->error('交易密码不正确！');
            }

            unset($data['trade_pwd']);

            // 验证
            $result = $this->validate($data, 'WithdrawalApi.edit');
            // 验证失败 输出错误信息
            if (true !== $result) {
                $this->error($result);
            }
            if ($data['status'] == 1) {
                $this->error('提现状态，请选择成功、失败！');
            }
            if (in_array($data['status'], [2, 3])) {
                if (!$data['remark']) {
                    $this->error('请输入本地备注！');
                }
            }

            if ($data['status'] == 2) {
                if ($data['now_cash'] < 1) {
                    $this->error('请输入（合理）到账金额！');
                }
                $data['now_cash'] = $data['now_cash'] * 100;
            }

            if (WithdrawalApiModel::update($data)) {

                $withdrawal = WithdrawalApiModel::where('order_id', $data['order_id'])->find();

                // 记录行为
                action_log('payment_withdrawal_api_edit', 'withdrawal', $withdrawal['order_id'], UID, json_encode($withdrawal));

                $this->success('操作成功', url('index'));
            } else {
                payment_error_log(860001, $data);
                $this->error('操作失败，更新接口提现状态失败！', url('index'));
            }
        }

        $all_withdrawal_status = [1 => '处理中', 2 => '成功', 3 => '失败'];
        // 所有的提现公司
        $all_config_group = pay_config('withdrawal_company');
        // 所有的提现公司子商户号
        $all_child_company = PaymentWithdrawalCompanyModel::order(['name' => 'asc'])->column('title', 'name');

        $info['company'] = isset($all_config_group[$info['company']]) ? $all_config_group[$info['company']] : '';
        $info['child_company'] = isset($all_child_company[$info['child_company']]) ? $all_child_company[$info['child_company']] : '';
        $info['return_param'] = format_nice($info['return_param']);
        $info['notify_param'] = format_nice($info['notify_param']);

        $items = [
            ['hidden', 'order_id', $id],
            ['static', 'merchant_id', '商户号', '商户名称：' . get_user_nickname($info['merchant_id'])],
            ['static', 'operation_id', '操作号', '商户名称：' . get_user_nickname($info['operation_id'])],
            ['static', 'bank_type', '银行卡类型', '', ['借记卡' => '借记卡', '信用卡' => '信用卡', '贷记卡' => '贷记卡']],
            ['static', 'bank_name', '收款银行名称'],
            ['static', 'name', '收款人姓名'],
            ['static', 'id_card', '收款人身份证'],
            ['static', 'mobile', '收款人手机号'],
            ['static', 'bank_card', '收款银行卡'],
            ['static', 'account_type', '账户类型', '', ['私户' => '私户', '公户' => '公户'], '私户'],
            ['static', 'account_opening_branch', '开户支行'],
            ['static', 'couplet_number', '联行号'],
            ['static', 'province', '省份名称'],
            ['static', 'city', '城市名称'],
            ['textarea', 'merchant_remark', '商户备注[:无]', '', '', 'disabled', ''],
            ['radio', 'status', '提现状态', '', $all_withdrawal_status, '', '', ''],
            ['static', 'cashs', '提现金额（元）', '', $info['cash'] / 100],
            ['static', 'order_id', '本地提现订单号'],
            ['text', 'now_cash', '到账金额', '实际上银行卡到账金额（请勿填错）'],
            ['text', 'other_order_id', '第三方订单号', '类似银行交易号，做凭证'],
            ['textarea', 'remark', '本地备注', '', '', '', ''],
            ['textarea', 'return_param', '返回数据[:无]', '', '', 'disabled', ''],
            ['textarea', 'notify_param', '通知数据[:无]', '', '', 'disabled', ''],
            ['static', 'company', '提现公司', '', '', '', ''],
            ['static', 'child_company', '提现公司子商户号'],
            ['datetime', 'create_time', '创建时间', '', '', 'YYYY-MM-DD HH:mm:ss', 'disabled'],
            ['password', 'trade_pwd', '交易密码', '6-16位'],
        ];

        $tips = "请勿填错！";

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('编辑') // 设置页面标题
            ->addFormItems($items)
            ->setPageTips($tips)
            ->setTrigger('status', '2', 'now_cash,other_order_id')
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }
}
